<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/main' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>参数管理</el-breadcrumb-item>
    </el-breadcrumb>
    <br />
    <br />

    <span>
      搜索：
      <el-input style="width:15rem" placeholder="参数主键" @input="search" v-model="search_name"></el-input>
    </span>
    <el-button type="primary" icon="el-icon-plus" @click="dialogVisible = true; isAdd=true">添加</el-button>

    <el-divider></el-divider>
    <el-table
      :data="tableData"
      style="width: 100%"
      class="table"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="50"></el-table-column>
      <el-table-column prop="param_cd" label="参数主键"></el-table-column>
      <el-table-column prop="param_value" label="参数值"></el-table-column>
      <el-table-column prop="description" label="描述"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="success" icon="el-icon-edit" size="mini" @click="edit(scope.row)">编辑</el-button>
          <el-button type="primary" icon="el-icon-delete" size="mini" @click="remove(scope.row, scope.$index)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <br />
    <el-button size="medium" type="primary" icon="el-icon-delete" @click="removeMore">批量删除</el-button>
    <el-dialog title="参数信息" :visible.sync="dialogVisible" width="30%">
      <el-form :model="paramForm" ref="paramForm" label-width="130px">
        <el-form-item
          label="参数主键"
          prop="param_cd"
          :rules="[{ required: true, message: '请输入参数主键'}]"
        >
          <el-input style="width:20rem" v-model="paramForm.param_cd" autocomplete="off"></el-input>
        </el-form-item>
        <br />
        <el-form-item
          label="描述"
          prop="description"
          :rules="[{ required: true, message: '请输入参数描述'}]"
        >
          <el-input style="width:20rem" v-model="paramForm.description" autocomplete="off"></el-input>
        </el-form-item>
        <br />
        <el-form-item
          label="参数值"
          prop="param_value"
          :rules="[{ required: true, message: '请输入参数值'}]"
        >
          <el-input style="width:20rem" v-model="paramForm.param_value" autocomplete="off"></el-input>
        </el-form-item>
        <br />
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm('paramForm')">提交</el-button>
        <el-button @click="cancel('paramForm')">取消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      dialogVisible: false,
      isAdd: false,
      search_name: "",
      tableData: [],
      multipleSelection: [],
      table: [],
      opRow: "",
      opIndex: "",
      paramForm: {
        param_cd: "",
        description: "",
        param_value: ""
      }
    };
  },
  mounted() {
    this.$post("/parameter/getParameters", {}).then(res => {
      //处理response
      console.log(res);
      if (res.code == 504) {
        this.$message.warning(res.message);
        return;
      }
      if (res.code == 200) {
        // this.$root.user_id=res.data.user_id;
        this.tableData = res.data;
        this.table = res.data;
      }
    });
  },
  methods: {
    search() {
      this.tableData = this.table.filter(e =>
        e.param_cd.match(this.search_name)
      );
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    refresh() {
      this.$post("/parameter/getParameters", {}).then(res => {
        //处理response
        console.log(res);
        if (res.code == 504) {
          this.$message.warning(res.message);
          return;
        }
        if (res.code == 200) {
          this.tableData = res.data;
          this.table = res.data;
        }
      });
    },
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          if (!this.isAdd) {
            // console.log(this.paramForm.param_value);
            this.$post("/parameter/updateParameter", {
              user_id: sessionStorage.getItem("user_id"),
              par_id: this.opRow.par_id,
              param_value: this.paramForm.param_value,
              description: this.paramForm.description,
              param_cd: this.paramForm.param_cd
            }).then(res => {
              // console.log(this.opRow.param_value);
              // console.log(this.paramForm.param_value);
              // console.log(res)
              if (res.code == 504) {
                this.$message.warning(res.message);
                return;
              }
              if (res.code == 200) {
                // this.$root.user_id=res.data.user_id;
                this.$message.success(res.message);
                this.refresh();
              }
            });

            this.dialogVisible = false;
            this.$refs[formName].resetFields();
            return;
          }
          if (this.isAdd) {
            // this.isAdd = false;
            this.$post("/parameter/addParameter", {
              user_id: sessionStorage.getItem("user_id"),
              param_value: this.paramForm.param_value,
              description: this.paramForm.description,
              param_cd: this.paramForm.param_cd
            }).then(res => {
              if (res.code == 504) {
                this.$message.warning(res.message);
                return;
              }
              if (res.code == 200) {
                // this.$root.user_id=res.data.user_id;
                this.$message.success(res.message);
                this.refresh();
              }
            });

            this.dialogVisible = false;
            this.$refs[formName].resetFields();
            return;
          }
        } else {
          return false;
        }
      });
    },
    cancel(formName) {
      this.$refs[formName].resetFields();
      this.dialogVisible = false;
    },
    edit(row) {
      this.isAdd = false;
      this.opRow = row;
      this.dialogVisible = true;
      this.$nextTick(function() {
        this.paramForm.param_cd = row.param_cd;
        this.paramForm.description = row.description;
        this.paramForm.param_value = row.param_value;
      });
    },
    removeMore() {
      if(this.multipleSelection == 0){
        this.$message.warning("请选择信息")
        return
      }
      this.multipleSelection.forEach(element => {
        this.$post("/parameter/deleteParameter", {
          par_id: element.par_id
        }).then(res => {
          if (res.code == 504) {
            this.$message.warning(res.message);
            return;
          }
          if (res.code == 200) {
            this.refresh();
          }
        });
      });
      this.$message.success("删除成功");
    },
    remove(row, index) {
      this.$post("/parameter/deleteParameter", {
        par_id: row.par_id
      }).then(res => {
        if (res.code == 504) {
          this.$message.warning(res.message);
          return;
        }
        if (res.code == 200) {
          this.$message.success(res.message);
          this.refresh();
        }
      });
    }
  }
};
</script>

<style scoped>
.table >>> .el-checkbox__input.is-indeterminate .el-checkbox__inner {
  background-color: #1598ec;
  border-color: #1598ec;
}
.table >>> .el-checkbox__input.is-checked .el-checkbox__inner {
  background-color: #1598ec;
  border-color: #1598ec;
}
.table >>> .el-checkbox__input.is-checked + .el-checkbox__label {
  color: #1598ec;
}
</style>